package cn.bigway.app.weavingdesigner.util;

import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;

public class ImageLoador {

	public static Image loadImage(String fileName) {
		LoadDialog loadDialog = new LoadDialog(fileName);
		loadDialog.pack();
		loadDialog.setVisible(true);
		Image image = loadDialog.getImage();
		loadDialog.release();
		loadDialog = null;
		return image;
	}

	public static void main(String[] args) {
		Image image = loadImage("d:\\aaa.jpg");
		System.out.println("image:" + image);
	}

}

@SuppressWarnings("serial")
class LoadDialog extends JDialog {

	private Image image;
	private JLabel label;
	private JProgressBar pb;

	public LoadDialog(final String fileName) {
		setUndecorated(true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setModal(true);
		JPanel panel = new JPanel(new FlowLayout());
		label = new JLabel("loading...  " + fileName);
		pb = new JProgressBar(0, 100);
		panel.add(label);
		panel.add(pb);
		getContentPane().add(panel);
		addWindowListener(new WindowListener() {
			@Override
			public void windowOpened(final WindowEvent arg0) {
				// TODO Auto-generated method stub

				SwingWorker<Void, Void> work = new SwingWorker<Void, Void>() {
					@Override
					protected Void doInBackground() throws Exception {

						MediaTracker mt = new MediaTracker(LoadDialog.this);
						try {
							image = Toolkit.getDefaultToolkit().createImage(
									fileName);
							mt.addImage(image, 0);
							mt.waitForAll(1);//
						} catch (Exception e) {
							System.out.println(e);
						}

						for (int i = 0; i < 10; i++) {
							label.setName(fileName);
							Thread.sleep(1000);
							pb.setValue(i * 10);
							pb.repaint();
							System.out.println(mt);
						}
						return null;
					}
					@Override
					protected void done() {
						// TODO Auto-generated method stub
						((LoadDialog) arg0.getSource()).setVisible(false);
						super.done();
					}
				};
				work.execute();
				System.out.println(arg0);
			}

			@Override
			public void windowIconified(WindowEvent arg0) {
				// TODO Auto-generated method stub
				System.out.println(arg0);
			}

			@Override
			public void windowDeiconified(WindowEvent arg0) {
				// TODO Auto-generated method stub
				System.out.println(arg0);
			}

			@Override
			public void windowDeactivated(WindowEvent arg0) {
				// TODO Auto-generated method stub
				System.out.println(arg0);
			}

			@Override
			public void windowClosing(WindowEvent arg0) {
				// TODO Auto-generated method stub
				System.out.println(arg0);
			}

			@Override
			public void windowClosed(WindowEvent arg0) {
				// TODO Auto-generated method stub
				System.out.println(arg0);
			}

			@Override
			public void windowActivated(WindowEvent arg0) {
				// TODO Auto-generated method stub
				System.out.println(arg0);
			}
		});

	}

	public void release() {
		image = null;
		dispose();
	}

	public Image getImage() {
		return image;
	}
}